﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Contents;

namespace MpSoft.Web.AdminCP.Controls.Content
{
    public partial class AdvEdit : System.Web.UI.UserControl
    {
        #region Var

        private int advid = 0;

        #endregion
        
        #region Method

        private void BindControl()
        {
            if (advid > 0)
            {
                var obj = AdvBUSS.Adv_ByID(advid);
                if (obj != null)
                {
                    txtTitle.Text = obj.Title;
                    txtDescription.Text = obj.Description;
                    if (ddlRegion.Items.FindByValue(obj.Region.ToString()) != null)
                        ddlRegion.Items.FindByValue(obj.Region.ToString()).Selected = true;
                    chkIsActive.Checked = obj.IsActive;
                    chkIsFlash.Checked = obj.IsFlash;
                    txtOrderSort.NumberIncremnet = obj.SortOrder;
                    txtImage.Text = obj.Image;
                    txtLink.Text = obj.Link;
                    txtWidth.Text = obj.Width.ToString();
                    txtHeight.Text = obj.Height.ToString();

                }
            }
        }

        private int Save()
        {
            Adv obj = null;
            if (advid > 0)
                obj = AdvBUSS.Adv_ByID(advid);
            if (obj == null)
                obj = new Adv();
            int value;
            int.TryParse(ddlRegion.SelectedValue, out value);
            obj.Title = txtTitle.Text;
            obj.Description = txtDescription.Text;
            obj.Region = value;
            obj.IsActive = chkIsActive.Checked;
            obj.IsFlash = chkIsFlash.Checked;
            obj.SortOrder = txtOrderSort.NumberIncremnet;
            obj.Image = txtImage.Text;
            obj.Link = txtLink.Text;
            int.TryParse(txtWidth.Text, out value);
            obj.Width = value;
            int.TryParse(txtHeight.Text, out value);
            obj.Height = value;
            if (advid > 0)
                value = AdvBUSS.Adv_Update(obj);
            else
                value = AdvBUSS.Adv_Add(obj);
            return value;
        }

        private void Message(string message, int type)
        {
            switch (type)
            {
                case 1:
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }
        }

        #endregion

        #region Event
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out advid);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }

        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=adv");
        }

        #endregion

        #region Properties
        #endregion
    }
}